#include<bits/stdc++.h>
using namespace std;
struct drink
{
	string name;
	string level;
};
int main()
{
	int N,M;
	cin >> N >> M ;
	vector<drink> a(N);
	for(int i=0;i<N;i++)
	cin >> a[i].name >> a[i].level;
	string temp;
	string ans;
	vector<string> b(M);
	for(int i=0;i<M;i++)
	cin >> b[i];
	int k=0;
	for(int idx=0;idx<M;idx++)
	{
		bool known =0;
		temp=b[idx];
		int count=0;
		for(int i=0;i<N;i++)
		{
			if(a[i].name==temp)
			{
				ans=a[i].level;
				known=1;
				break;
			}
		}
		if(known)
		{
			cout << ans <<endl;
			continue;
		}
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<N;j++)
			{
				string temp_name=a[i].name+a[j].name;
				if(temp_name==temp)
				{
					count++;
					if (count == 1) 
					{
                    	ans = a[i].level + a[j].level;
                   	} 
					else 
					{
                    count = 2;
                    break;
                    }
				}
			}
		}
		if(count==1)
		cout << ans << endl;
		else 
		cout << "D" <<endl;
	}
	return 0;
}